package com.street.service.impl;

import com.street.constant.MessageConstant;
import com.street.constant.StatusConstant;
import com.street.dto.EmpLoginDTO;
import com.street.entity.Employee;
import com.street.entity.Shop;
import com.street.exception.AccountLockedException;
import com.street.exception.AccountNotFoundException;
import com.street.exception.PasswordErrorException;
import com.street.mapper.EmployeeMapper;
import com.street.mapper.ShopMapper;
import com.street.service.EmployeeService;
import com.street.vo.EmpLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    EmployeeMapper employeeMapper;
    @Autowired
    ShopMapper shopMapper;

    @Override
    public Employee login(EmpLoginDTO empLoginDTO) {
        //先从mapper层获取用户名密码,再和传来的用户名密码比对
        String username=empLoginDTO.getUsername();
        String password=empLoginDTO.getPassword();
        Employee employee= employeeMapper.getByUsername(username);
        if(employee==null)throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (employee.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        //3、返回实体对象
        return employee;



    }

    @Override
    public Shop selectShopByUsername(String username) {
        Employee employee = employeeMapper.getByUsername(username);
        Shop shop = shopMapper.selectByShopId(employee.getShopId());
        return shop;
    }
}
